package com.samoiloff.ui.btns {
	import com.samoiloff.owncrosswords.viewers.classic.Settings;
	import com.slavshik.utils.Fonts;

	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 * @author Damirr
	 */
	public class SimpleTextBtnController extends EventDispatcher {

		private var _tf : TextField;
		private var _mc : MovieClip;
		private var _bg : BtnBg;

		public function SimpleTextBtnController(mc : MovieClip, label : String = "Label") {
			_mc = mc;
			_bg = new BtnBg();
			_mc.addChildAt(_bg, 0);
			_tf = mc.tf;
			Fonts.getInstance().applyTfTextFormat(_tf, Settings.getInstance().btnFormat);
			_tf.mouseEnabled = false;
			
			this.label = label;
			//trace("BrightTextBtnController.BrightTextBtnController(); " + _tf.text);
			_mc.useHandCursor = _mc.buttonMode = true;
			
			_mc.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			_mc.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			_mc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			_mc.addEventListener(MouseEvent.CLICK, onClick);
		}

		private function onMouseDown(e : MouseEvent) : void {
			_bg.frame = 3;
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
		}

		private function onClick(e : MouseEvent) : void {
			_bg.frame = 2;
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}

		private function onOut(e : MouseEvent) : void {
			_bg.frame = 1;
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
		}

		private function onOver(e : MouseEvent) : void {
			_bg.frame = 2;
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
		}

		public function destroy() : void {
			_mc.removeEventListener(MouseEvent.MOUSE_OVER, onOver);
			_mc.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			_mc.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_mc.removeEventListener(MouseEvent.CLICK, onClick);
			_mc = null;
			_tf = null;
			_bg = null;
		}

		public function get mc() : MovieClip {
			return _mc;
		}

		
		public function get label() : String {
			return _tf.text;
		}

		public function set label(label : String) : void {
			_tf.text = label;
		}
	}
}
